package com.example.mongo.appmongo.service;

import com.example.mongo.appmongo.dao.IAppDao;
import com.example.mongo.appmongo.entity.App;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;
import java.util.UUID;

/**
 * @author sjw
 * @className HomeService
 * @date 2021/9/29 14:41
 * @description: TODO
 */

@Service
public class HomeService {

    @Autowired
    private IAppDao appDao;

    public String insert(String name){
        App app=new App();
        app.setName("hello");
        appDao.save(app);

        return app.getId();

    }

//    @Transactional(rollbackFor = Exception.class, transactionManager = "#{@tenantContext.getTenant()}")
    @Transactional(rollbackFor = Exception.class)
    public String tran(String name,String id) throws Exception {
//        // 615415944cfa5e0640ea0f96
        Optional<App> appOptional= appDao.findById(id);
        if(appOptional.isPresent()){
        App app=appOptional.get();
        app.setName(app.getName()+"1");
        appDao.save(app);}


        Thread.sleep(2000);

        App app1=new App();
//        app1.setId("123456");
        app1.setName("hello");
        appDao.save(app1);


        if("123".equals(name))
        throw new Exception();
        return app1.getId();
    }


    public String get(String name) throws Exception {
//        // 615415944cfa5e0640ea0f96
        Optional<App> appOptional= appDao.findById("615415944cfa5e0640ea0f96");

        App app=appOptional.get();
        app.setName(app.getName()+"1");
        appDao.save(app);



        return app.getId();
    }

}
